package sbc.ejb3;

import java.util.Date;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.validator.NotNull;

import sbc.ejb3.dao.CallbackHandler;

@Entity
@EntityListeners({CallbackHandler.class})
@Table(name="Platine")
@NamedQueries({
    @NamedQuery(
        name="UnmontiertePlatinen",
        query=
            "SELECT p " +
            "FROM Platine p " +
            "WHERE p.firstSensor IS NULL AND " +
            "p.secondSensor IS NULL AND " +
            "p.ausgeliefertAm IS NULL"
    ),
    @NamedQuery(
        name="MontiertePlatinen",
        query=
            "SELECT p " +
            "FROM Platine p " +
            "WHERE p.firstSensor IS NOT NULL AND " +
            "p.secondSensor IS NOT NULL AND " +
            "p.ausgeliefertAm IS NULL"
    )
})
public class Platine implements java.io.Serializable {
	
	private static final long serialVersionUID = 1L;
	
	@Id @GeneratedValue
    @Column(name="messungId")
	private Integer id;
	
    @Column(name="platineId")
    @NotNull
	private String platineId;
    
    @Column
    private String mitarbeiterId;
    
    @Column
    private Date ausgeliefertAm;
	
	@OneToOne(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    @JoinColumn(name="firstSensorId", nullable=true, updatable=true, unique=false)
	private Sensor firstSensor;
	
	@OneToOne(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    @JoinColumn(name="secondSensorId", nullable=true, updatable=true, unique=false)
	private Sensor secondSensor;	
		
	public Platine() {}	

	public Platine(String platineId) {
		super();
		this.platineId = platineId;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getPlatineId() {
		return platineId;
	}

	public void setPlatineId(String platineId) {
		this.platineId = platineId;
	}

	public Sensor getFirstSensor() {
		return firstSensor;
	}

	public void setFirstSensor(Sensor firstSensor) {
		this.firstSensor = firstSensor;
	}

	public Sensor getSecondSensor() {
		return secondSensor;
	}

	public void setSecondSensor(Sensor secondSensor) {
		this.secondSensor = secondSensor;
	}
	
	public String getMitarbeiterId() {
		return mitarbeiterId;
	}

	public void setMitarbeiterId(String mitarbeiterId) {
		this.mitarbeiterId = mitarbeiterId;
	}

	public Date getAusgeliefertAm() {
		return ausgeliefertAm;
	}

	public void setAusgeliefertAm(Date ausgeliefertAm) {
		this.ausgeliefertAm = ausgeliefertAm;
	}

	public String toString() {
		String string = platineId;
		if (id != null)
			string += " (" + id + ")";
		if (mitarbeiterId != null)
			string += " m: " + mitarbeiterId;
		if (ausgeliefertAm != null)
			string += " a: " + ausgeliefertAm;
		if (firstSensor != null)
			string += " s1: " + firstSensor.toString();
		if (secondSensor != null)
			string += " s2: " + secondSensor.toString();		
		return string;		
	}
}